<template>
  <div class="login-account">
    <el-form
      ref="formRef"
      label-width="auto"
      label-position="top"
      :rules="rules"
      :model="account"
    >
      <el-form-item prop="email">
        <template #label>Email</template>
        <el-input placeholder="Email Adress" v-model="account.email"></el-input>
      </el-form-item>

      <el-form-item prop="password" class="el-form-password">
        <template #label>Password</template>
        <el-input
          placeholder="Password"
          v-model="account.password"
          show-password
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { useStore } from '@/store'
import { ElForm } from 'element-plus'

import { rules } from '../config/account-config'
// import { ILoginAccountProps } from './types/index'

import localCache from '@/utils/cache'

const props = withDefaults(
  defineProps<{
    isKeep?: boolean
  }>(),
  {
    isKeep: true,
  }
)

const store = useStore()
const account = reactive({
  email: localCache.getCache('email') ?? '',
  password: localCache.getCache('password') ?? '',
})

const formRef = ref<InstanceType<typeof ElForm>>()

const loginAction = () => {
  // 1.判断用户名和密码
  formRef.value?.validate((valid: boolean) => {
    if (!valid) {
      return
    }
    // 2.判断是否记住密码
    if (props.isKeep) {
      localCache.setCache('email', account.email)
      localCache.setCache('password', account.password)
    } else {
      localCache.deleteCache('email')
      localCache.deleteCache('password')
    }
    // 3.执行登录请求
    store.dispatch('login/accountLoginAction', { ...account })
  })
}

defineExpose({ loginAction })
</script>

<style scoped lang="less">
.el-form-password {
  margin-top: 30px;
}
:deep(.el-form-item__label) {
  font-size: 18px;
  font-family: SF Pro Display;
  font-weight: 400;
  color: #707070;
}

:deep(.el-input__inner) {
  height: 55px;
  border-radius: 10px;
  padding-left: 17px !important;
}
</style>
